/*
 * PrecioHoraPane.java
 *
 * Created on 26 de junio de 2006, 10:46 AM
 */

package inc.jmc.cyberboard.ztrashcan;
import inc.jmc.cyberboard.common.NumFormat;

import javax.swing.*;
import java.awt.*;
import javax.swing.border.TitledBorder;
/**
 *
 * @author  juolguin
 */
public class PrecioHoraSegmentClockPane extends javax.swing.JPanel {
    

    private javax.swing.border.TitledBorder titBorder1=javax.swing.BorderFactory.createTitledBorder(null, "primera hora", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14), new java.awt.Color(204, 0, 0));

    public void setTitBorder1(TitledBorder titBorder1) {
        this.titBorder1 = titBorder1;
    }

    public TitledBorder getTitBorder1() {
        return titBorder1;
    }

    
    private javax.swing.table.DefaultTableModel dtm=null;
    java.text.NumberFormat nf2= java.text.NumberFormat.getInstance();
    /** Creates new form PrecioHoraPane */
    public PrecioHoraSegmentClockPane(String titulo) {
        initComponents();
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, titulo, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14)));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][]{},new String[]{}));
        this.updateUI();
        nf2.setMaximumFractionDigits(1);
        nf2.setMinimumFractionDigits(1);
        
        
    }
    void setTarifaIntervalos(int divisiones,Float precioIncremento)
    {
        String []  titulos=new String [divisiones+10];
        Object [][] content=new Object [1][divisiones];
        ////
        //estableciendo los titulos para la tabla
        float increm=60.0f/(float)divisiones;
        
        for(int i=0;i<divisiones;++i)
        {
            
            float inf=increm*i;
            /*if(inf==1)
                inf=0;*/
            float sup=increm*(i+1);
            titulos[i]="["+nf2.format(inf)+" - "+nf2.format(sup)+" ]mins";
            if(precioIncremento!=null)
            {
                    content[0][i]=NumFormat.money(precioIncremento.floatValue()*(i+1));
                
            }
            else
            {
                content[0][i]="";
            }
        
        }
        
        dtm=new javax.swing.table.DefaultTableModel(content,titulos);
        jTable1.setModel(dtm);
        jTable1.setPreferredSize(new java.awt.Dimension(80*titulos.length,60));
        jTable1.setMaximumSize(new java.awt.Dimension(80*titulos.length,60));
        jTable1.setMinimumSize(new java.awt.Dimension(80*titulos.length,60));
        if(jTable1.getTableHeader()!=null)
          jTable1.getTableHeader().setReorderingAllowed(false);
        jTable1.setColumnSelectionAllowed(false);
        jTable1.setCellSelectionEnabled(false);
        int max=jTable1.getColumnModel().getColumnCount();
        jTable1.getColumnModel().setColumnMargin(0);
        
        for(int i=0;i<max;++i)
        {
            jTable1.getColumnModel().getColumn(i).setResizable(true);
        }
        //despues lo que tenemos que hacer es deshabilitar la edicion de las columnas
        //a la derecha, que ya no son parte de la division de horas
        //por eso ponemos un JTextField sin permisos para editar, y lo establecemos como el 
        //editor de celda por default para esas columnas
        JTextField aux1=new javax.swing.JTextField();
        aux1.setEditable(false);
        javax.swing.DefaultCellEditor aux2=new javax.swing.DefaultCellEditor(aux1);
        for(int i=0;i<10;++i)
        {
            jTable1.getColumnModel().getColumn(divisiones+i).setCellEditor(aux2);
        }
        jScrollPane1.setViewportView(jTable1);
        jScrollPane1.updateUI();
        
    
    
    
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        dividirHoraTxt = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        usarPrecioChk = new javax.swing.JCheckBox();
        precioTxt = new javax.swing.JTextField();
        updateBtn = new javax.swing.JButton();
        borrarBtn = new javax.swing.JButton();

        setLayout(null);

        setPreferredSize(new java.awt.Dimension(540, 140));
        jPanel1.setLayout(null);

        jPanel1.setBorder(getTitBorder1());
        jLabel1.setText("Dividir hora en");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(20, 20, 90, 14);

        dividirHoraTxt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                dividirHoraTxtKeyReleased(evt);
            }
        });

        jPanel1.add(dividirHoraTxt);
        dividirHoraTxt.setBounds(50, 40, 50, 19);

        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setToolTipText("Especifica el precio dentro de la celda");
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTable1.setMaximumSize(new java.awt.Dimension(300, 64));
        jTable1.setMinimumSize(new java.awt.Dimension(300, 64));
        jScrollPane1.setViewportView(jTable1);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(120, 20, 390, 60);

        usarPrecioChk.setText("<html>Usar precio <BR>\nincremento\n</html>");
        usarPrecioChk.setToolTipText("Esto copia el mismo precio en cada celda");
        usarPrecioChk.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        usarPrecioChk.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel1.add(usarPrecioChk);
        usarPrecioChk.setBounds(20, 60, 80, 30);

        precioTxt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                precioTxtKeyReleased(evt);
            }
        });

        jPanel1.add(precioTxt);
        precioTxt.setBounds(50, 90, 50, 19);

        updateBtn.setMnemonic('c');
        updateBtn.setText("Actualizar");
        updateBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateBtnActionPerformed(evt);
            }
        });

        jPanel1.add(updateBtn);
        updateBtn.setBounds(120, 90, 100, 23);

        borrarBtn.setText("Quitar esta tarifa");
        borrarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                borrarBtnActionPerformed(evt);
            }
        });

        jPanel1.add(borrarBtn);
        borrarBtn.setBounds(380, 90, 130, 23);

        add(jPanel1);
        jPanel1.setBounds(10, 10, 520, 130);

    }// </editor-fold>//GEN-END:initComponents

    private void precioTxtKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_precioTxtKeyReleased

        if(evt.getKeyCode()==java.awt.event.KeyEvent.VK_ENTER)
        {
        
            updateBtnActionPerformed(null);
        }
    }//GEN-LAST:event_precioTxtKeyReleased

    private void borrarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_borrarBtnActionPerformed

        javax.swing.JPanel pan =(javax.swing.JPanel)getParent();
        pan.remove(this);
        pan.updateUI();
        
    }//GEN-LAST:event_borrarBtnActionPerformed

    private void updateBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateBtnActionPerformed

        Float precio=null;
        if(usarPrecioChk.isSelected())
        {
            try{
            precio=new Float(Float.parseFloat(precioTxt.getText()));
            }
            catch(NumberFormatException ex)
            {
                precio=null;
            
            }
            
        }
        int npartes=Integer.parseInt(dividirHoraTxt.getText());
        setTarifaIntervalos(npartes, precio);
        
    }//GEN-LAST:event_updateBtnActionPerformed

    private void dividirHoraTxtKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_dividirHoraTxtKeyReleased

        if(evt.getKeyCode()==java.awt.event.KeyEvent.VK_ENTER)
        {
        
            updateBtnActionPerformed(null);
        }
    }//GEN-LAST:event_dividirHoraTxtKeyReleased
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton borrarBtn;
    private javax.swing.JTextField dividirHoraTxt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField precioTxt;
    private javax.swing.JButton updateBtn;
    private javax.swing.JCheckBox usarPrecioChk;
    // End of variables declaration//GEN-END:variables
    
}
